//
//  WZGShowNoworryView.m
//  ZCJZ
//
//  Created by 潘洪波 on 2019/12/20.
//  Copyright © 2019 jeking. All rights reserved.
//

#import "WZGShowNoworryView.h"

@interface WZGShowNoworryView ()<UIGestureRecognizerDelegate>

@property (nonatomic, weak) IBOutlet UIView *bgView ;
@property (weak, nonatomic) IBOutlet UILabel *protocolTitleLab;
@property (weak, nonatomic) IBOutlet UITextView *protocolContentTxView;

@property (nonatomic, assign) CGRect myFrame ;
@property (nonatomic, copy) void (^callBack)(void) ;
@property (nonatomic, strong) WZGProtocolModel *protocol ;

@end


@implementation WZGShowNoworryView

+ (void)showView:(WZGProtocolModel*)protocol WithComplete:(void (^)(void))complete{
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    WZGShowNoworryView* view = [[WZGShowNoworryView alloc] initWithFrame:keyW.bounds];
    
    view.callBack = complete;
    
    view.protocol = protocol;
    [keyW addSubview:view];
    [view showView];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UINib *nib = [UINib nibWithNibName:@"WZGShowNoworryView" bundle:nil];
        self = [nib instantiateWithOwner:self options:nil].firstObject;
        self.myFrame = frame;
        [self setUpCustomview];
    }
    return self;
}

- (void)setProtocol:(WZGProtocolModel *)protocol{
    if (protocol==nil) {
        return;
    }
    _protocol = protocol;
    self.protocolTitleLab.text = protocol.protocol_name;
    self.protocolContentTxView.attributedText = [[NSAttributedString alloc] initWithData:[protocol.pop_up_content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
}

- (void)setUpCustomview{
    
    self.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    self.bgView.userInteractionEnabled = YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([touch.view isDescendantOfView:self.bgView]) {
        return NO;
    }
    
    return YES;
}


- (void)showView{
    self.bgView.transform = CGAffineTransformMakeScale(0.00001, 0.00001);
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.transform = CGAffineTransformMakeScale(1, 1);
    } ];
}

- (void)hideView{
    [self hideView:nil];
}

- (void)hideView:(void(^)(bool finish))complete{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bgView.transform = CGAffineTransformMakeScale(0.00001, 0.00001);
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    } completion:^(BOOL finished) {
        [self.bgView removeFromSuperview];
        [self removeFromSuperview];
        if (complete) {
            complete(YES);
        }
    }];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = _myFrame;
}

- (IBAction)confirmAction:(id)sender {
    [self hideView:^(bool finish) {
        if (self.callBack) {
            self.callBack();
        }
    }];
}

@end
